package ee.greaton.siteManager.GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ee.greaton.siteManager.DB.SQLiteConnection;
import ee.greaton.siteManager.model.Site;

public class AddNewGUI extends Thread {
	Site site;
	JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8;
	boolean editMode = false;

	public AddNewGUI(Site site) {
		this.site = site;
		start();
	}

	public void run() {
		JLabel empty = new JLabel("");
		JFrame frame = new JFrame();
		frame.setTitle("Site Manager - Lisa uus sait");
		frame.setSize(300, 300);
		JPanel panel = new JPanel();
		GridLayout gl = new GridLayout(0, 2);
		panel.setLayout(gl);
		JLabel lbl1 = new JLabel(" Saidi nimi: ");
		tf1 = new JTextField();
		JLabel lbl2 = new JLabel(" Baasi URL: ");
		tf2 = new JTextField();
		JLabel lbl3 = new JLabel(" Baasi kasutaja: ");
		tf3 = new JTextField();
		JLabel lbl4 = new JLabel(" Baasi parool: ");
		tf4 = new JTextField();
		JLabel lbl5 = new JLabel(" Baasi nimi: ");
		tf5 = new JTextField();
		JLabel lbl6 = new JLabel(" FTP URL: ");
		tf6 = new JTextField();
		JLabel lbl7 = new JLabel(" FTP kasutaja: ");
		tf7 = new JTextField();
		JLabel lbl8 = new JLabel(" FTP parool: ");
		tf8 = new JTextField();
		JButton btn1 = new JButton();
		btn1.setText("Salvesta");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				site = new Site();
				site.setName(tf1.getText());
				site.setDbUrl(tf2.getText());
				site.setDbUser(tf3.getText());
				site.setDbPass(tf4.getText());
				site.setDbName(tf5.getText());
				site.setFtpUrl(tf6.getText());
				site.setFtpUser(tf7.getText());
				site.setFtpPass(tf8.getText());
				if(editMode){
					SQLiteConnection.getInstance().updateSite(site);
				}else{
					SQLiteConnection.getInstance().addSite(site);
				}
			}
		});
		if (site != null) {
			editMode = true;
			tf1.setText(site.getName());
			tf2.setText(site.getDbUrl());
			tf3.setText(site.getDbUser());
			tf4.setText(site.getDbPass());
			tf5.setText(site.getDbName());
			tf6.setText(site.getFtpUrl());
			tf7.setText(site.getFtpUser());
			tf8.setText(site.getFtpPass());
		}
		panel.add(lbl1);
		panel.add(tf1);
		panel.add(lbl2);
		panel.add(tf2);
		panel.add(lbl3);
		panel.add(tf3);
		panel.add(lbl4);
		panel.add(tf4);
		panel.add(lbl5);
		panel.add(tf5);
		panel.add(lbl6);
		panel.add(tf6);
		panel.add(lbl7);
		panel.add(tf7);
		panel.add(lbl8);
		panel.add(tf8);
		panel.add(empty);
		panel.add(btn1);
		frame.add(panel);
		frame.setVisible(true);
	}
}
